﻿using System;
using System.Collections.Generic;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE.Survivor
{
    public class NpcUnit: BattleUnit
    {
        protected enum ENpcState
        {
            Idle,
            Escape,
            Squad,
            WaitingForRescue,
            Rescued,
        }

        public BattleRpgNpcConfig npcConfig
        {
            get;
            protected set;
        }
        protected ENpcState state;
        private bool isRescued = false;
        public int pointId;

        public void InitRescue()
        {
            state = ENpcState.WaitingForRescue;
            isRescued = false;
            // hudComponent = AddComponent<HudComponent>();
            // hudComponent.Initialize(this);
        }

        public void OnRescued()
        {
            isRescued = true;
        }
        
        public override void InitHero()
        {
            npcConfig = BattleConfigManager.Instance.GetNpcConfig(heroId);
            hudOffset = npcConfig.hud_offset;
            resId = npcConfig.res_id;
            state = ENpcState.Idle;
            searchTeamMask = BattleHelper.GetTeamMask(0);
            var ratio = StageManager.Instance.missionConfig.attribute_ratio;
            var attackRatio = StageManager.Instance.missionConfig.attribute_ratio_atk * ratio;
            var defRatio = StageManager.Instance.missionConfig.attribute_ratio_def * ratio;
            var hpRatio = StageManager.Instance.missionConfig.attribute_ratio_hp * ratio;
            totalHp = (int)(npcConfig.hp * hpRatio);
            if (totalHp == 0)
            {
                BattleDebug.LogError("totalHp is 0 id: " + heroId);
                totalHp = 10;
            }
            currentHp = totalHp;
            defence = (int)(npcConfig.def * defRatio);
            attack = (int)(npcConfig.atk * attackRatio);
            collisionRadius = npcConfig.radius;
            configMoveSpeed = npcConfig.move_speed;
           
            
        }

        public override void InitComponents()
        {
            renderComponent = AddComponent<RenderComponent>();
            if (SurvivorManager.isNavMesh)
            {
                navmeshComponent = AddComponent<NavmeshComponent>();
            }
            hudComponent = AddComponent<HudComponent>();
            AddComponent<CollisionComponent>();
        }

        public override void ShowBubble()
        {
            
        }

        protected virtual void UpdateAI()
        {
            if (state == ENpcState.Idle)
            {
                if (aiType == EEnemyAIType.SearchTarget)
                {
                    SearchTarget();
                }
                else
                {
                    SearchBattlePoint();
                }
                
                if (target != null || behaviorState == EBehaviorState.Battle)
                {
                    if (pointId > 0)
                    {
                        var point = StageManager.Instance.GetMapPoint(pointId);
                        TargetPosition = point;
                        navmeshComponent?.SetAgentTargetPosition(point);
                        //Play("Escape");
                        //SetSpeed(1);
                        Play("Move");
                        state = ENpcState.Escape;
                    }
                    else
                    {
                        Play("Fear");
                        state = ENpcState.Squad;
                        //SetNavmeshAgentActive(false);
                    }
                }
            }
            else if (state == ENpcState.Escape)
            {
                if (Vector3.Distance(TargetPosition, position) < 0.5f)
                {
                    
                    Play("Fear");
                    state = ENpcState.Squad;
                    //SetNavmeshAgentActive(false);
                }
            }
            else if (state == ENpcState.WaitingForRescue)
            {
                TargetForward = manualMovingDirection;
                if (isRescued)
                {
                    Play("Move");
                    //state = ENpcState.Rescued;
                }
                else
                {
                    Play("Hijack");
                }
            }
            /*else if (state == ENpcState.Rescued)
            {
                TargetForward = manualMovingDirection;
            }*/
        }
        
        protected override void OnUpdate()
        {
            UpdateAI();
            base.OnUpdate();
        }

        private Vector3 lastPosition;
        
        public override void StartAim(BattleUnit target = null,bool useStartPoint = true)
        {
            
        }
    }
}
